package ar.com.ford.it.fullEconomics.persistence.dao;

import java.io.InputStream;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.ford.it.logging.ILogger;
import com.ford.it.logging.LogFactory;
import com.ford.it.persistence.PersistenceConfigurationException;
import com.ford.it.persistence.PersistenceHelper;
import com.ford.it.properties.PropertyException;
import com.ford.it.properties.PropertyManager;

public class JndiInitialize implements ServletContextListener {
    private static final String CLASS_NAME = JndiInitialize.class.getName();

    private static ILogger log = LogFactory.getInstance().getLogger(CLASS_NAME);
    private static PropertyManager propertyManager;
    private static PropertyManager propertyCommonManager;
    private String jobRunningServer = null;

    public void contextDestroyed(ServletContextEvent arg0) {
    }

    public void contextInitialized(ServletContextEvent arg0) {
       propertyManager = PropertyManager.getInstance();
        try {
            /*propertyManager.initDynaProp();
            propertyManager.loadFromDB("dbconnection_fullee");
            String user = null;// pm.getGroup("DBConnectionCT").getString("logonId");
            String password = null;
            PropertyGroup pg = (PropertyGroup) propertyManager
                    .getGroup("dbconnection_fullee");
            
            user = pg.getString("User");
            password = pg.getString("Password");*/
          
            InputStream stream = Dao.class.getResourceAsStream("/resources/persistence-config.xml");
            propertyManager.load(stream);
/*            PropertyGroup root = propertyManager.getChildGroup("PersistenceConfig");
            root = root.getGroup("PersistenceApp");
            
            root.addProperty("logonId", user);
            root.addProperty("logonPassword", password);*/
            
            
   //         System.out.println("El usuario es:" + user);
     //       System.out.println("El pass es:" + password);
        /*}  catch (PropertyResourceException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();*/
        } catch (PropertyException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    
    public static PropertyManager getInstance()
    {
        return propertyManager;
    }

    public void init() throws PersistenceConfigurationException {
        try {
            System.out.println("Before Persistence");
            
            
            PersistenceHelper.initialize("resources/persistence-config.xml");

            System.out.println("After Persistence");
        } catch (Exception e) {
            System.out.println("the persistence Fullee can't initialize");
            e.printStackTrace();
        }

        // Connection con=this.getConnection(propertyManager);
        // System.out.println("la conexion es:::"+con);

        this.log = LogFactory.getInstance()
                .getLogger(this.getClass().getName());
    }
   
}
